import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    userInfo: JSON.parse(sessionStorage.getItem('userInfo'))
  },
  mutations: {
    // set
    SET_TOKEN: (state, token)=>{
      state.token = token;
      // 将token存入浏览器的localstorage中，
      localStorage.setItem("token", token);
    },
    SET_USERINFO: (state, userInfo)=>{
      state.userInfo = userInfo;
      // 将userInfo存入浏览器的sessionStorage中，浏览器关闭之后就没了
      sessionStorage.setItem("userInfo", JSON.stringify(userInfo)); // 将userInfo序列化成字符串
    },
    REMOVE_INFO: (state)=>{
      state.token = '';
      state.userInfo = {};
      localStorage.setItem("token", '');
      sessionStorage.setItem("userInfo", '');
    }
  },
  getters: {
    // get
    getUser: state=>{
      return state.userInfo;
    }
  },
  actions: {
  },
  modules: {
  }
})
